/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundaries;

import utils.ImageLoader;
import controllers.CalendarManager;
import entities.Event;
import entities.OutdoorEvent;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
 
import org.primefaces.event.ScheduleEntryMoveEvent;
import org.primefaces.event.ScheduleEntryResizeEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.LazyScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

/**
 *
 * @author Alberto, Alessandra
 */
@ManagedBean
@ViewScoped
public class CalendarBean implements Serializable {
    
    private static final Logger logger = Logger.getLogger("CalendarBean");
    
    @EJB
    private CalendarManager cm;
    
    private ScheduleModel eventModel;
 
    private ScheduleEvent event = new DefaultScheduleEvent();
 
    @PostConstruct
    public void init() {
        //log info
        logger.log(Level.INFO, "Calendar bean created");
        
        eventModel = new DefaultScheduleModel();
        
        ArrayList<Event> events =  cm.listOfEvents();
        Iterator<Event> iterevents = events.iterator();
        while(iterevents.hasNext()){
            Event e = iterevents.next();
            DefaultScheduleEvent se = new DefaultScheduleEvent(e.getName(), e.getStartTime(), e.getEndTime());
            se.setData(e);
            eventModel.addEvent(se);
        }
    }
     
    public ScheduleModel getEventModel() {
        return eventModel;
    }
     
    public ScheduleEvent getEvent() {
        return event;
    }
 
    public void setEvent(ScheduleEvent event) {
        this.event = event;
    }
    
    public void addEvent(ActionEvent actionEvent) {
        if(event.getId() == null)
            eventModel.addEvent(event);
        else
            eventModel.updateEvent(event);
         
        event = new DefaultScheduleEvent();
    }
     
    public void onEventSelect(SelectEvent selectEvent) {
        //log info
        if (selectEvent.getObject() == null)
            logger.log(Level.INFO, "null");
        
        event = (ScheduleEvent) selectEvent.getObject();
    }
     
    public void onDateSelect(SelectEvent selectEvent) {
        event = new DefaultScheduleEvent("", (Date) selectEvent.getObject(), (Date) selectEvent.getObject());
    }
    
    public String modifyEvent () {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedevent", event.getData());
        return "updateevent?faces-redirect=true";
    }
    
    public String eventDetails () {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedevent", event.getData());
        return "eventdetails?faces-redirect=true";
    }
    
    public void delete () {
        cm.deleteEvent((Event) event.getData());
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("calendar.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(CalendarBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public String getType () {
        if (event.getData() instanceof OutdoorEvent)
            return "Outdoor";
        else return "Indoor";
    }
    
    public String getWeatherForecast () {
        if (event.getData() instanceof OutdoorEvent && ((OutdoorEvent)event.getData()).isForecasted()) {
            return ImageLoader.getWeatherImg(((OutdoorEvent)event.getData()).getWeatherforecast().getName());
        } else {
            return "";
        }
    }
    
    public Boolean isCreator () {
        if (event.getData() == null)
            return false;
        if (cm.isCreator((Event)event.getData()))
            return true;
        else return false;
    }
    
    public Boolean isFuture () {
        if (event.getData() == null)
            return false;
        else {
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(((Event)event.getData()).getDate());
            GregorianCalendar todaycal = new GregorianCalendar();
            if (cal.compareTo(todaycal) > 0) {
                return true;
            } else {
                return false;
            }
        }
    }
    
}